/*
 * 文 件    名:  BaseService.java
 * 创建日期:  2012-10-10下午2:22:52
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作          者:  dc051
 */
package com.weipu.common.facade.content;

import java.util.Arrays;

import android.database.Cursor;

import com.weipu.common.util.Logger;

/** 
 * @Description service 超类
 * @version [版本号, 2012-10-10]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author dc051
 */
public class BaseService
{
    
    /**
     * 参数校验
     * @param parameter
     * @return [参数说明]
     * @return boolean [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    protected boolean check(Object... parameter)
    {
        for (int i = 0; i < parameter.length; i++)
        {
            if (null == parameter[i] || "".equals(parameter[i]))
            {
                return false;
            }
        }
        return true;
    }
    
    /**
     * 操作返回码
     */
    private int returnCode = 0;
    
    /**
     * 操作返回信息
     */
    private String returnMsg = "success";
    
    /**
     * 获取操作返回码
     * @return [参数说明]
     * @return int [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public int getReturnCode()
    {
        return returnCode;
    }
    
    /**
     * 设置接口调用后的返回码
     * @param returnCode [参数说明]
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    protected void setReturnCode(int returnCode)
    {
        this.returnCode = returnCode;
    }
    
    /**
     * 操作返回信息
     * @return [参数说明]
     * @return String [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public String getReturnMsg()
    
    {
        return returnMsg;
    }
    
    /**
     * 设置接口调用的备注信息
     * @param returnMsg [参数说明]
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    protected void setReturnMsg(String returnMsg)
    {
        this.returnMsg = returnMsg;
    }
    
    /**
     * close the cursor
     * @param cursor [参数说明]
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    protected void close(Cursor cursor)
    {
        if (null != cursor)
        {
            cursor.close();
        }
    }
    
    /**
     *
     * validate the cursor is null or empty
     * @param cursor
     * @return [参数说明]
     * @return boolean [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    protected boolean isNullOrEmpty(Cursor cursor)
    {
        return null == cursor || !cursor.moveToFirst();
    }
    
    /**
     * query by raw sql
     * @param sql
     * @param parameter
     * @return Cursor [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public Cursor rawQuery(String sql, String... parameter)
    {
        
        Logger.d("#sql", "sql: " + sql + " ==> parameter: " + Arrays.toString(parameter));
        return ContentProviderFactory.getContentProvier(ContentURI.CONTENT_URI_RAW_SQL).rawQuery(sql, parameter);
    }
}
